<template>
  <div class="role">
    <el-card shadow="never">
      <el-form :inline="true" label-width="80px">
        <el-form-item label="优惠卷码">
          <el-input v-model="paramFrom.couponCode" size="mini" />
        </el-form-item>
        <el-form-item label="状态">
          <!--          <el-input v-model="paramFrom.status" size="mini"/>-->
          <el-select v-model="paramFrom.status" placeholder="请选择" size="mini">
            <el-option v-for="item in statusList" :key="item.id" :label="item.value" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="query">查询</el-button>
        </el-form-item>
        <el-form-item style="float: right">
          <el-button type="primary" size="mini" @click="addCoupon">新增优惠卷</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card shadow="never" style="margin-top: 20px;">
      <!--      <el-row type="flex" justify="end" style="margin-bottom: 15px;">-->
      <!--        <el-button type="primary" size="mini" @click="btnOK">添加员工</el-button>-->
      <!--      </el-row>-->
      <el-table border :data="tableData" style="width: 100%">

        <el-table-column prop="couponCode" align="center" label="优惠卷" width="200px" />
        <el-table-column prop="status" align="center" label="状态">
          <template v-slot="{ row }">
            <span v-if="row.status === 0">未使用</span>
            <span v-if="row.status === 1">已下发</span>
            <span v-if="row.status === 2">已使用</span>
            <span v-if="row.status === 3">已过期</span>
          </template>
        </el-table-column>
        <el-table-column prop="value" align="center" label="优惠值" />
        <el-table-column prop="challengeProject" align="center" label="挑战项目">
          <template v-slot="{ row }">
            <span v-if="row.challengeProject === 0">评估</span>
            <span v-if="row.challengeProject === 1">即时</span>
            <span v-if="row.challengeProject === 2">固定</span>
            <span v-if="row.challengeProject === 3">比赛</span>
            <span v-if="row.challengeProject === 4">股票</span>
            <span v-if="row.challengeProject === 5">免费评估</span>
            <span v-if="row.challengeProject === 6">通用</span>
          </template>
        </el-table-column>
        <el-table-column prop="accountMoney" align="center" label="账户规模" />
        <!-- <el-table-column prop="scopeOfUse" align="center" label="类型">
          <template v-slot="{ row }">
            <span v-if="row.scopeOfUse === 1">挑战</span>
            <span v-if="row.scopeOfUse === 2">联盟</span>
          </template>
        </el-table-column> -->
        <el-table-column prop="creationTime" align="center" label="创建时间" width="200px" />
        <el-table-column align="center" label="操作">
          <template v-slot="{ row }">
            <el-button type="text" @click="delBtn(row)">删除</el-button>
          </template>
        </el-table-column>

      </el-table>
      <paging :page-size="paramFrom.pageSize" :total="total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
      <!-- 删除 和 修改的对话框 -->
      <el-dialog title="删除优惠卷" :visible="showVisible" width="30%" :before-close="handleClose">
        <div style="text-align: center;line-height: 30px">
          <span style="font-size: 16px;">你确定要删除这个优惠卷吗</span>
          <div style="font-size: 16px;">优惠卷: {{ couponCode }}</div>
        </div>

        <el-row type="flex" justify="center" style="margin-top: 20px;">
          <el-button size="mini" @click="handleClose">取 消</el-button>
          <el-button size="mini" type="primary" @click="delBtnOK">确 定</el-button>
        </el-row>
      </el-dialog>
      <el-dialog title="新增优惠卷" :visible.sync="dialogFormVisibleCoupon" width="30%">
        <el-form :model="ruleForm" label-width="30%" :rules="rules" ref="ruleForm">
          <!-- <el-form-item label="类型" label-width="80px" prop="scopeOfUse">
            <el-select v-model="ruleForm.scopeOfUse" autocomplete="off" size="mini">
              <el-option label="挑战" value="1" />
              <el-option label="联盟" value="2" />
            </el-select>
          </el-form-item> -->

          <el-form-item label="挑战项目" label-width="80px">
            <el-select v-model="ruleForm.challengeProject" size="mini" placeholder="挑战项目">
              <el-option v-for=" item  in  challengeProjectList " :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>

          <el-form-item label="账户规模" label-width="80px">
            <el-select v-model="ruleForm.accountMoney" size="mini" placeholder="挑战项目">
              <el-option v-for=" item  in  accountMoney " :key="item.id" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>

          <el-form-item label="优惠值" label-width="80px" prop="value">
            <el-input v-model="ruleForm.value" autocomplete="off" class="topbut botbut" size="mini"
              placeholder="请输入0-0.9的值" type="number"></el-input>
          </el-form-item>
          <el-form-item label="优惠卷" label-width="80px" prop="couponCode">
            <el-input v-model="ruleForm.couponCode" autocomplete="off" size="mini"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisibleCoupon = false" size="mini">取 消</el-button>
          <el-button type="primary" @click="submitAddCoupon" size="mini">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { deleteCoupon, getCouponList, randomCoupon, submitAddCoupon } from '@/api/staff'
import paging from '@/components/paging'

export default {
  name: 'VueAdminIndex',
  components: {
    paging,
  },
  data() {
    return {
      dialogFormVisibleCoupon: false, // 新增优惠卷弹出框
      paramFrom: {
        couponCode: null, // 优惠卷码
        page: 1,
        pageSize: 10,
        status: null, // 状态
      },
      showVisible: false,
      tableData: [
        {
          status: 0
        }
      ],
      ruleForm: {
        couponCode: null, //优惠卷码
        value: null, // 优惠卷折扣数值
        challengeProject: null,  // 
        accountMoney: null
      },
      status: 0,
      total: 0,
      // 参数
      dialogVisible: false,
      currid: null,
      couponCode: null,
      staffid: null,
      statusList: [
        { id: null, value: '全部' },
        { id: 0, value: '未使用' },
        { id: 1, value: '已下发' },
        { id: 2, value: '已使用' },
        { id: 3, value: '已过期' },
      ],
      // rules: {
      //   scopeOfUse: [
      //     { required: true, message: '请选择', trigger: 'change' }
      //   ],
      // },      
      challengeProjectList: [
        {
          id: null,
          name: "全部"
        },
        {
          id: 0,
          name: '评估'
        },
        {
          id: 1,
          name: '即时'
        },
        {
          id: 2,
          name: '固定'
        },
        {
          id: 3,
          name: '比赛'
        },
        {
          id: 4,
          name: '股票'
        },
        {
          id: 5,
          name: '免费评估'
        }
      ],
      accountMoney: [

      ]
    }
  },

  mounted() {
    this.getCouponList()
  },

  methods: {
    // 后期要改
    handleSizeChange(val) {
      this.paramFrom.pageSize = val
      this.getCouponList()
    },
    handleCurrentChange(val) {
      this.paramFrom.page = val
      this.getCouponList()
    },
    // 删除
    delBtn(row) {
      this.staffid = row.id
      this.couponCode = row.couponCode
      this.showVisible = true
    },
    // 取消
    handleClose() {
      this.showVisible = false
    },
    async delBtnOK() {
      console.log('this.staffid', this.staffid)
      this.$confirm('你确定要删除吗?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await deleteCoupon(this.staffid)
        this.showVisible = false
        this.$message({
          type: 'success',
          message: '删除成功'
        })
        // this.showVisible = false
        this.getCouponList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    /**
     * @author 焦延龙 <19131965027@163.com>
     * @description 新增优惠卷弹出框
     * @date 2024/3/18
     */
    addCoupon() {
      this.ruleForm.value = null
      this.dialogFormVisibleCoupon = true
      this.randomCoupon()
    },

    /**
     * @author 焦延龙 <19131965027@163.com>
     * @description 确定新增优惠卷
     * @date 2024/3/18
     */
    async submitAddCoupon() {
      if (this.ruleForm.scopeOfUse === null) {
        this.$message({
          type: 'error',
          message: '请选择优惠卷类型'
        })
        return
      }
      if (this.ruleForm.value < 0) {
        this.$message({
          type: 'error',
          message: '优惠值不能小于0'
        })
        return
      }
      if (this.ruleForm.value >= 1) {
        this.$message({
          type: 'error',
          message: '优惠值不能大于或等于1'
        })
        return
      }
      await submitAddCoupon(this.ruleForm)
      await this.getCouponList()
      this.dialogFormVisibleCoupon = false
    },

    /**
     * @author 焦延龙 <19131965027@163.com>
     * @description 回显随机优惠卷
     * @date 2024/3/18
     */
    async randomCoupon() {
      const res = await randomCoupon()
      // 优惠卷
      this.ruleForm.couponCode = res.couponCode
      let newArr = [...res.accountMoney]
      let arr = []
      arr.push({
        value: null,
        label: "全部"
      })
      for (let i = 0; i < newArr.length; i++) {
        let item = newArr[i]
        arr.push({
          value: item,
          label: item
        })
      }
      this.accountMoney = arr
    },

    /**
     * @author 焦延龙 <19131965027@163.com>
     * @description 优惠卷分页查询
     * @date 2024/3/18
     */
    async getCouponList() {
      const res = await getCouponList(this.paramFrom)
      this.tableData = res.records
      this.total = res.total
    },
    // 搜索
    query() {
      this.getCouponList()
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .topbut input::-webkit-outer-spin-button,
::v-deep .topbut input::-webkit-inner-spin-button {
  -webkit-appearance: none;
}

::v-deep .topbut input[type="number"] {
  -moz-appearance: textfield;
}

::v-deep .botbut inpit {
  border: none
}
</style>
